package com.xuexuehai.bkm.service.user.admin.handler;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.xuexuehai.bkm.core.common.constants.RespCode;
import com.xuexuehai.bkm.core.common.dto.HttpResp;
import com.xuexuehai.bkm.core.common.exception.security.*;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.time.LocalDateTime;

/**
 * @author xuexuehai
 * @mailBox xuehai.xue@QQ.com
 * @date 2021/9/12 10:51 上午
 */
@RestControllerAdvice
public class AuthorityExceptionHandler {

    @ExceptionHandler(UsernameIsEmptyException.class)
    public HttpResp userNameIsEmptyHandler(UsernameIsEmptyException e){
        return new HttpResp.Builder()
                .code(RespCode.USERNAME_IS_EMPTY.getCode())
                .msg(e.getMessage())
                .times(LocalDateTime.now())
                .builder();
    }

    @ExceptionHandler(InvalidUserException.class)
    public HttpResp invalidUserHandler(InvalidUserException e){
        return new HttpResp.Builder()
                .code(RespCode.INLAID_USER.getCode())
                .msg(e.getMessage())
                .times(LocalDateTime.now())
                .builder();
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    public HttpResp usernameNotFoundHandler(UsernameNotFoundException e){
        return new HttpResp.Builder()
                .code(RespCode.USERNAME_NOT_FOUND.getCode())
                .msg(e.getMessage())
                .times(LocalDateTime.now())
                .builder();
    }

    @ExceptionHandler(AccessNotPermitException.class)
    public HttpResp accessNotPermitHandler(AccessNotPermitException e){
        return new HttpResp.Builder()
                .code(RespCode.DO_NOT_HAVE_ACCESS_PRIVS.getCode())
                .msg(e.getMessage())
                .times(LocalDateTime.now())
                .builder();
    }

    @ExceptionHandler(UserNotLoginException.class)
    public HttpResp userNotLoginHandler(UserNotLoginException e){
        return new HttpResp.Builder()
                .code(RespCode.HAS_NOT_LOGIN.getCode())
                .msg(e.getMessage())
                .times(LocalDateTime.now())
                .builder();
    }

    @ExceptionHandler(JWTVerificationException.class)
    public HttpResp tokenHandler(JWTVerificationException e){
        return new HttpResp.Builder()
                .code(RespCode.JWT_VILIFICATION_FAIL.getCode())
                .msg(e.getMessage())
                .times(LocalDateTime.now())
                .builder();
    }
}
